package game.module.sss.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.hall.logic.CheatManager;
import game.module.sss.bean.SssRoom;
import game.module.sss.logic.SssDataManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageSss.C2SSSSJoinRoomCheck;
import lua.LuaMessageSss.S2CSSSJoinRoomCheck;

@MsgCodeAnn(msgcode = C2SSSSJoinRoomCheck.id, accessLimit = 500)
public class SssJoinRoomCheckProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssJoinRoomCheckProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SSSSJoinRoomCheck reqMsg = C2SSSSJoinRoomCheck.parse(request);
		String room_id = reqMsg.room_id;
		int playerId = hero.getId();
		logger.info("sss join room check,playeyId={},room_id={}", playerId, room_id);
		// 是否在房间中
		boolean isInRoom = SssDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fang not exist
		if (!SssDataManager.getInstance().checkRoomExist(room_id)) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		final SssRoom ddzRoom = SssDataManager.getInstance().getSssRoom(room_id);
		// 人数已满

		int fangka = hero.getPlayerBean().getFang_ka();
		int fangka_cost = ddzRoom.getRoomFee();
		if (ddzRoom.getRoomFeeType() == 2 && fangka < fangka_cost)// 赢家收费
		{
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (ddzRoom.getRoomFeeType() == 1 && fangka < fangka_cost / 2) {// AA
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}

		if (ddzRoom.getPlayerList().size() >= ddzRoom.getPlayer_count()) {
			S2ErrorCode respMsg = new S2ErrorCode(105);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (ddzRoom.getIs_jindou() == 1 && hero.getPlayerBean().getJin_dou() < ddzRoom.getJindou() * ddzRoom.getRoundCount() * 5) {// 金豆房间
			S2ErrorCode respMsg = new S2ErrorCode(133);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (CheatManager.getInstance().checkCheatAndPush(ddzRoom.getPrevent_cheat(), ddzRoom.getIps(), hero)) {
			return;
		}
		// ret
		S2CSSSJoinRoomCheck respMsg = new S2CSSSJoinRoomCheck();
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
